/*
 * DHCPHost.java Created on 31 Январь 2006 г., 14:59 To change this template, choose Tools | Template Manager and open
 * the template in the editor.
 */

package org.dicr.sys.linux;

import java.io.*;
import java.util.regex.*;

import org.dicr.util.data.*;
import org.dicr.util.net.*;

/**
 * DHCP Host element of dhcp file.
 * 
 * @author Igor A Tarasov &lt;linux@dicr.org&gt;
 * @version 060131
 */
public class DHCPHost implements Serializable {
	/** Serial ID */
	private static final long serialVersionUID = 1L;

	/** Name max length */
	private static final int NAME_MAXLENGTH = 15;

	private static final Pattern NAME_PATTERN = Pattern.compile(String.format("^[a-z\\d]{1,%d}$",
	        Integer.valueOf(DHCPHost.NAME_MAXLENGTH)));

	private String name = null;

	private MAC mac = null;

	private IP ip = null;

	/**
	 * Constructor.
	 * 
	 * @param hostName host name
	 * @param theMac MAC-address
	 * @param theIp IP-address
	 */
	protected DHCPHost(final String hostName, final MAC theMac, final IP theIp) {
		String theName = StringUtils.normalize(hostName);
		if (theName == null) throw new IllegalArgumentException("empty name");
		theName = theName.toLowerCase();
		if (!DHCPHost.NAME_PATTERN.matcher(theName).matches()) throw new IllegalArgumentException("name: " + theName);
		if (theMac == null) throw new IllegalArgumentException("null mac");
		if (theIp == null) throw new IllegalArgumentException("null ip");
		this.name = theName;
		this.mac = theMac;
		this.ip = theIp;
	}

	/**
	 * Return host name.
	 * 
	 * @return host name
	 */
	public String getName() {
		return this.name;
	}

	/**
	 * Return host IP.
	 * 
	 * @return IP-address
	 */
	public IP getIp() {
		return this.ip;
	}

	/**
	 * Return host MAC.
	 * 
	 * @return MAC-address
	 */
	public MAC getMac() {
		return this.mac;
	}

	/**
	 * Convert to string.
	 */
	@Override
	public String toString() {
		final StringBuilder sb = new StringBuilder("DHCPHost[");
		sb.append("mac=").append(this.mac);
		if (this.ip != null) {
			sb.append("ip=").append(this.ip);
		}
		sb.append("]");
		return sb.toString();
	}
}
